﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameServer
{
    public partial class Config2 : Form
    {
        private List<TextBox> lstPrecet;
        List<int> m_eachPre = new List<int>();
        public Config2()
        {
            InitializeComponent();

            lstPrecet = new List<TextBox>();

            lstPrecet.Add(this.tbPrecent0);
            lstPrecet.Add(this.tbPrecent1);
            lstPrecet.Add(this.tbPrecent2);
            lstPrecet.Add(this.tbPrecent3);
            lstPrecet.Add(this.tbPrecent4);
            lstPrecet.Add(this.tbPrecent5);
            lstPrecet.Add(this.tbPrecent6);
            lstPrecet.Add(this.tbPrecent7);
            lstPrecet.Add(this.tbPrecent8);
            lstPrecet.Add(this.tbPrecent9);
            lstPrecet.Add(this.tbPrecent10);
            lstPrecet.Add(this.tbPrecent11);
            lstPrecet.Add(this.tbPrecent12);
            lstPrecet.Add(this.tbPrecent13);
            lstPrecet.Add(this.tbPrecent14);
            lstPrecet.Add(this.tbPrecent15);
            lstPrecet.Add(this.tbPrecent16);
            lstPrecet.Add(this.tbPrecent17);
            lstPrecet.Add(this.tbPrecent18);
            lstPrecet.Add(this.tbPrecent19);
            lstPrecet.Add(this.tbPrecent20);
            lstPrecet.Add(this.tbPrecent21);
            lstPrecet.Add(this.tbPrecent22);
            lstPrecet.Add(this.tbPrecent23);
        }

        private void Config2_Load(object sender, EventArgs e)
        {
            InitPei();
            InitSubPei();
            InitPrecent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void InitPei()
        {
            this.tbPei1.KeyPress += tbPrecent_KeyPress;
            this.tbPei2.KeyPress += tbPrecent_KeyPress;
            this.tbPei3.KeyPress += tbPrecent_KeyPress;
            this.tbPei4.KeyPress += tbPrecent_KeyPress;
            this.tbPei5.KeyPress += tbPrecent_KeyPress;
            this.tbPei6.KeyPress += tbPrecent_KeyPress;
            this.tbPei7.KeyPress += tbPrecent_KeyPress;
            this.tbPei8.KeyPress += tbPrecent_KeyPress;

            this.tbPei1.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Apple).iGivenNum.ToString();
            this.tbPei2.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Orange).iGivenNum.ToString();
            this.tbPei3.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Mango).iGivenNum.ToString();
            this.tbPei4.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Bell).iGivenNum.ToString();
            this.tbPei5.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Watermelon).iGivenNum.ToString();
            this.tbPei6.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Star).iGivenNum.ToString();
            this.tbPei7.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Qi).iGivenNum.ToString();
            this.tbPei8.Text = FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Bar).iGivenNum.ToString();
        }

        void InitSubPei()
        {
            this.tbNum2.KeyPress += tbPrecent_KeyPress;
            this.tbNum3.KeyPress += tbPrecent_KeyPress;
            this.tbNum5.KeyPress += tbPrecent_KeyPress;
            this.tbNum8.KeyPress += tbPrecent_KeyPress;
            this.tbNum11.KeyPress += tbPrecent_KeyPress;
            this.tbNum14.KeyPress += tbPrecent_KeyPress;
            this.tbNum17.KeyPress += tbPrecent_KeyPress;
            this.tbNum20.KeyPress += tbPrecent_KeyPress;
            this.tbNum23.KeyPress += tbPrecent_KeyPress;

            this.tbNum2.Text = FruitDataManager.Intstance.GetdefaultPrice(2).ToString();
            this.tbNum3.Text = FruitDataManager.Intstance.GetdefaultPrice(3).ToString();
            this.tbNum5.Text = FruitDataManager.Intstance.GetdefaultPrice(5).ToString();
            this.tbNum8.Text = FruitDataManager.Intstance.GetdefaultPrice(8).ToString();
            this.tbNum11.Text = FruitDataManager.Intstance.GetdefaultPrice(11).ToString();
            this.tbNum14.Text = FruitDataManager.Intstance.GetdefaultPrice(14).ToString();
            this.tbNum17.Text = FruitDataManager.Intstance.GetdefaultPrice(17).ToString();
            this.tbNum20.Text = FruitDataManager.Intstance.GetdefaultPrice(20).ToString();
            this.tbNum23.Text = FruitDataManager.Intstance.GetdefaultPrice(23).ToString();
        }

        void InitPrecent()
        {
            List<int> lst = FruitDataManager.Intstance.GetPresentLst();
            for (int index = 0; index < lst.Count; index++)
            {
                lstPrecet[index].KeyPress += tbPrecent_KeyPress;
                lstPrecet[index].Text = lst[index].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Apple).iGivenNum = GetInt(this.tbPei1.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Orange).iGivenNum = GetInt(this.tbPei2.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Mango).iGivenNum = GetInt(this.tbPei3.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Bell).iGivenNum = GetInt(this.tbPei4.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Watermelon).iGivenNum = GetInt(this.tbPei5.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Star).iGivenNum = GetInt(this.tbPei6.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Qi).iGivenNum = GetInt(this.tbPei7.Text.Trim());
            FruitDataManager.Intstance.GetFruitGiven(FruitEnum.Bar).iGivenNum = GetInt(this.tbPei8.Text.Trim());

            FruitDataManager.Intstance.GetFruitData(2).iNum = GetInt(this.tbNum2.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(3).iNum = GetInt(this.tbNum3.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(5).iNum = GetInt(this.tbNum5.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(8).iNum = GetInt(this.tbNum8.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(11).iNum = GetInt(this.tbNum11.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(14).iNum = GetInt(this.tbNum14.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(17).iNum = GetInt(this.tbNum17.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(20).iNum = GetInt(this.tbNum20.Text.Trim());
            FruitDataManager.Intstance.GetFruitData(23).iNum = GetInt(this.tbNum23.Text.Trim());

            FruitDataManager.Intstance.GetPresentLst().Clear();

            for (int index = 0; index < lstPrecet.Count; index++)
            {
                FruitDataManager.Intstance.GetPresentLst().Add(GetInt(lstPrecet[index].Text));
            }

            FruitDataManager.Intstance.Save();

            this.Close();

        }

        private int GetInt(string id)
        {
            id = id.Trim();
            int ret = -1;
            try
            {
                ret = int.Parse(id);
            }
            catch (System.Exception ex)
            {
            	
            }

            return ret;
        }

        private void cb_Precent_CheckedChanged(object sender, EventArgs e)
        {
            SetPrecentShow(this.cb_Precent.Checked);
        }

        private void SetPrecentShow(bool bePrecent)
        {
            if (bePrecent)
            {
                m_eachPre.Clear();
                int total = 0;
                for (int index = 0; index < lstPrecet.Count; index++)
                {
                    lstPrecet[index].Enabled = false;
                    int num = GetInt(lstPrecet[index].Text);
                    total += num;
                    m_eachPre.Add(num);
                }

                for (int index = 0; index < lstPrecet.Count; index++)
                {
                    float precent = m_eachPre[index] * 1.0f / total;
                    lstPrecet[index].Text = string.Format("{0:P}", precent);
                }
            }
            else
            {
                for (int index = 0; index < lstPrecet.Count; index++)
                {
                    lstPrecet[index].Enabled = true;
                    lstPrecet[index].Text = m_eachPre[index].ToString();
                }
            }
        }

        private void tbPrecent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if ((e.KeyChar < '0') || e.KeyChar > '9')
                {
                    e.Handled = true;
                }
            }
        }
    }
}
